﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using WpfApp4.Views;

namespace WpfApp4.Models
{
    public static class Notify
    {
        public static MainWindow _mainWindow;

        private static NotifyIcon notifyIcon = new NotifyIcon()
        {
            BalloonTipText = "12312",
            Text = "321321",
            //Icon = new System.Drawing.Icon("ReSources\\0e16uhj9x80zyp26z0jhg28wegkfgsb5.ico"),
            ContextMenuStrip = GetMenu()
        };

        /// <summary>
        /// 添加右下角角标
        /// </summary>
        public static void AddNewNotify()
        {
            notifyIcon.Visible = true;
            notifyIcon.MouseClick += OnNotifyClick;
            notifyIcon.MouseDoubleClick += OnNotifyClick;
            notifyIcon.ShowBalloonTip(1000);
        }

        private static void OnNotifyClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                notifyIcon.ContextMenuStrip.Hide();
                _mainWindow.Show();
                _mainWindow.WindowState = System.Windows.WindowState.Normal;
            }
        }

        private static ContextMenuStrip GetMenu()
        {
            const int Item_Window = 0;
            const int Item_Muted = 1;
            const int Item_Exit = 5;

            ContextMenuStrip cms = new ContextMenuStrip();
            ToolStripMenuItem item_window = new ToolStripMenuItem("主界面") { Tag = Item_Window };
            item_window.Click += OnMenuItemClick;

            ToolStripMenuItem item_muted = new ToolStripMenuItem("静音") { Tag = Item_Muted };
            item_muted.Click += OnMenuItemClick;

            ToolStripMenuItem item_exit = new ToolStripMenuItem("退出") { Tag = Item_Exit };
            item_exit.Click += OnMenuItemClick;

            cms.Items.Add(item_window);
            cms.Items.Add(item_muted);
            cms.Items.Add(item_exit);

            return cms;

            void OnMenuItemClick(object sender, EventArgs e)
            {
                switch ((int)((ToolStripMenuItem)sender).Tag)
                {
                    case Item_Exit:
                        _mainWindow.Close();
                        Dispose();
                        Application.Exit();
                        Environment.Exit(0);
                        break;
                    case Item_Muted:
                        break;
                    case Item_Window:
                        break;
                }
            }
        }

        public static void Dispose()
        {
            notifyIcon.Visible = false;
            notifyIcon.Dispose();
        }
    }
}
